﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ArbolandiaBL;
using System.Web.Security;

public partial class Admin_Login : System.Web.UI.Page
{
    protected void Page_PreLoad(object sender, EventArgs e)
    {
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Initialization.SetTheme();
        }
        ////TEST purpouse comment this lines
        //txtPassword.Text = "123";
        //txtUsername.Text = "ermez3";
        //btnLogin_Click(null, null);
    }
    protected void btnLogin_Click(object sender, DirectEventArgs e)
    {
        string userNameValue = txtUsername.Text.Trim();
        string passwordValue = txtPassword.Text.Trim();


        if (userNameValue.Equals("") || passwordValue.Equals(""))
        {
            X.Msg.Show(new MessageBoxConfig { Title = "Alerta", Message = "Usuario y Password son Requeridos", Buttons = MessageBox.Button.OK, Icon = MessageBox.Icon.WARNING });
        }
        else
        {
            var validLogin = Security.FirstLogin(userNameValue, passwordValue);
            if (validLogin)
            {
                FormsAuthentication.Authenticate(userNameValue, passwordValue);
                Response.Redirect("Administrator.aspx");
            }
            else
            {
                X.Msg.Show(new MessageBoxConfig { Title = "Acceso Invalido", Message = "Usuario y/o contraseña Invalidos", Buttons = MessageBox.Button.OK, Icon = MessageBox.Icon.ERROR });
            }
        }
    }
}